package com.lewis.module.m2.api.record.controller;

import com.lewis.common.core.entity.BaseResult;
import com.lewis.common.mongodb.MongodbUtil;
import com.lewis.module.m2.api.record.entity.AutoReply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自动回复控制器
 *
 * @author Lewis 398529803@qq.com 2022年8月9日, 009 15:24:43
 */
@Api(tags = "qq机器人")
@RestController
@RequestMapping("/record/qq/bot/auto/reply")
public class RecordAutoReplyController {

	@Resource
	private MongodbUtil mongodbUtil;

	@ApiOperation("模糊查询一个结果")
	@GetMapping("/getOne")
	public BaseResult getOne(AutoReply autoReply) {
		if (autoReply.getCommand() == null || autoReply.getCommand().equals("")) {
			return BaseResult.fail().setMsg("参数不完整，无法查询");
		}
		Criteria criteria = new Criteria();
		criteria.andOperator(
				Criteria.where("command").regex(".*?" + autoReply.getCommand() + ".*")
		);
		List<AutoReply> autoReplyList = mongodbUtil.getRandomByCriteria(criteria, AutoReply.class, 1);
		if (autoReplyList != null && autoReplyList.size() != 0) {
			AutoReply one = autoReplyList.get(0);
			return BaseResult.ok().setData(one);
		}
		return BaseResult.fail().setMsg("未查询到数据");
	}

	@ApiOperation("添加自动回复")
	@PostMapping("/add")
	public BaseResult add(@RequestBody AutoReply autoReply) {
		autoReply.setId(new ObjectId().toString());
		return BaseResult.ok().setData(mongodbUtil.save(autoReply));
	}
}
